package cn.schoolwow.quickdao.test.module.business.user.dml.userRole.flow.common;

import cn.schoolwow.quickdao.domain.DAO;
import cn.schoolwow.quickdao.test.entity.Role;
import cn.schoolwow.quickdao.test.entity.User;
import cn.schoolwow.quickdao.test.entity.UserRole;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import org.junit.Assert;

public class CheckUserRoleResultFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        checkUser(flowContext);
        checkRole(flowContext);
    }

    @Override
    public String name() {
        return "检查用户角色表结果流程";
    }

    private void checkUser(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        User insertUser = (User) flowContext.checkData("insertUser");

        User user = (User) dao.query(User.class)
                .addQuery("id", insertUser.getId())
                .joinTable(UserRole.class, "id", "userId")
                .done()
                .execute()
                .getOne();
        Assert.assertEquals(insertUser.getId(), user.getId());
        Assert.assertEquals(insertUser.getUsername(), user.getUsername());
    }

    private void checkRole(FlowContext flowContext){
        DAO dao = flowContext.checkInstanceData(DAO.class);
        Role insertRole = (Role) flowContext.checkData("insertRole");

        Role role = (Role) dao.query(Role.class)
                .addQuery("id", insertRole.getId())
                .joinTable(UserRole.class, "id", "roleId")
                .done()
                .execute()
                .getOne();
        Assert.assertEquals(insertRole.getId(), role.getId());
        Assert.assertEquals(insertRole.getRoleName(), insertRole.getRoleName());
    }
}
